<?php
    class Plugins_Poll extends model {

    
    function getformadd($productid="")
  {
  
$code  = '<div class="wrpPlg clBg5 clBd2 bdFull">'."\n";
$code .= '<div class="wrpOutAtcPlg">'."\n";
$code .= '<div class="wrpAtcPlg">'."\n";
$code .= '<span class="ico3 icoPollPlg"></span>'."\n";
$code .= '<div class="spnAtcPlg clTxtW">'."\n";
$code .= '<span class="txtAtcPlg fs03">Poll</span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<div class="wrpTl">'."\n";
$code .= '<div title="Close" class="btnIco closeTl clBd3 mgBtm10 deleterowbutton">'."\n";
$code .= '<span class="ico icoCloseTl"></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '<form class="frmFrmPlg frmQustPlg clTxt3 fs03">'."\n";
$code .= '<fieldset class="wrpFrmPlg">'."\n";
$code .= '<table class="tbGenInfo tbAddCtnt clTxt3 fs03">'."\n";
$code .= '<tbody><tr>'."\n";
$code .= '<th>User Type</th>'."\n";
$code .= '<td>'."\n";

$code .= '<div class="userRsv">'."\n";



$code .= '<input type="radio" id="gubpoll" value="0" name="userpoll">'."\n";
$code .= '<label for="mbpoll">Guest</label>'."\n";



$code .= '<input type="radio" id="mbpoll" value="1" name="userpoll">'."\n";
$code .= '<label for="mbpoll">Member</label>'."\n";



$code .= '<input type="radio" id="pmuserpoll" value="2" name="userpoll">'."\n";
$code .= '<label for="pmuserpoll">Member Plus</label>'."\n";



$code .= '</div>'."\n";

$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr>'."\n";
$code .= '<th>Poll Title</th>'."\n";
$code .= '<td>'."\n";

$code .= '<input name="questitle" type="text" class="clBg5 clBd2 fs03 clTxt2" kl_virtual_keyboard_secure_input="on">'."\n";

$code .= '<input name="spollid" type="hidden" >'."\n";


$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '<tr class="wrpChoiQust">'."\n";
$code .= '<th>Choice</th>'."\n";
$code .= '<td >'."\n";

$code .= '<div id="elechoice">'."\n";

$code .= '</div>'."\n";



$code .= '<div class="addChoiQust">'."\n";
$code .= '<input type="button" name="choicebtn" value="Add Choice" class="btnGen clTxtW cutTxt clBgBt1 fs04">'."\n";
$code .= '</div>'."\n";


$code .= '<div class="choiOthQust">'."\n";
$code .= '<input name="otherpoll" value="1" type="checkbox">'."\n";
$code .= '<label class="">Other</label>'."\n";
$code .= '</div>';


$code .= '</td>'."\n";
$code .= '</tr>'."\n";
$code .= '</tbody></table>'."\n";
$code .= '</fieldset>'."\n";
$code .= '</form>'."\n";
$code .= '</div>';

$arraydata['form']=addslashes(str_replace(array("\r", "\n", "\r"), '', $code));

return $arraydata;
  
  }
  
  
  function getplugin($plugreid)
  {
  
 
     $sql="select * from tb_plugins_poll where plugreid=".$plugreid;
     $arrayplugin=$this->db->db_set_1recordrow($sql);
     
     $sql='select * from tb_plugins_poll_form where spollid='.$arrayplugin['spollid'];
     
     $arrayform=$this->db->db_set_recordset($sql);
     
     
     $arrayplugin['form']=$arrayform;
     

  
     return $arrayplugin;

  
  
  

  
  }
  
   function getformlanding($arraydata)
   {

 $code  = '<div class="ldIt wrpFrmLd clBg4" id="pollfidset-'.$arraydata['spollid'].'"><div id="pollvoteset-'.$arraydata['spollid'].'" class="wrpHdInfoGen">'."\n";
$code .= '<h2 class="clTxt2 fs04 boldGen">Poll</h2>'."\n";
$code .= '<div class="wrpInFrmLd">'."\n";
$code .= '<h2 class="boldGen clTxt3 fs04">'.$arraydata['title'].'</h2>'."\n";
$code .= '<div class="wrpPollInLd">'."\n";

if(count($arraydata['form']))
{
foreach($arraydata['form'] as $valueform)
{
$code .= '<div class="itFrmLd radioPollLd clearfix">'."\n";
$code .= '<div class="pollLst clearfix">'."\n";
$code .= '<input type="radio" name="pollfid" value="'.$valueform['pollfid'].'" id="rdoOpt3">'."\n";
$code .= '<label for="rdoOpt3" class="clTxt3 fs03">'.$valueform['value'].'</label>'."\n";
$code .= '<span></span>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
}
}
if($arraydata['other']=='1')
{
$code .= '<div class="itFrmLd radioPollLd clearfix">'."\n";
$code .= '<div class="pollLst clearfix">'."\n";
$code .= '<input type="radio" name="pollfid" value="0" id="rdoOpt5">'."\n";
$code .= '<label for="rdoOpt5" class="clTxt3 fs03">Other</label>'."\n";
$code .= '<input type="text" name="othername" class="clBg5 clBd2 fs03 clTxt3">'."\n";
$code .= '</div>'."\n";
$code .= '</div>';
}



$code .= '<div class="sendPoll">'."\n";
$code .= '<input type="button" onclick="pollfunction('.$arraydata['productid'].','.$arraydata['spollid'].')" value="VOTE" class="btnGen clTxtW cutTxt clBgSp2 fs04">'."\n";

$code .= '<input type="button" onclick="pollresultfunction('.$arraydata['spollid'].',1)" value="Result" class="btnGen clTxtW cutTxt clBgSp2 fs04">'."\n";

$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";






$code .= '</div>';




$code .= '<div class="wrpHdInfoGen" id="pollvoteset2-'.$arraydata['spollid'].'" style="display:none">'."\n";
$code .= '<h2 class="clTxt2 fs04 boldGen">Poll</h2>'."\n";
$code .= '<div class="wrpInFrmLd">'."\n";
$code .= '<h2 class="boldGen clTxt3 fs04">'.$arraydata['title'].'</h2>'."\n";
$code .= '<div class="wrpPollInLd">'."\n";
$code .= '<div class="wrpPollOutLd">'."\n";








$code .= '</div>'."\n";

$code .= '<div class="sendPoll">'."\n";
$code .= '<input type="button" onclick="pollresultfunction('.$arraydata['spollid'].',2)" value="Back To Vote" class="btnGen clTxtW cutTxt clBgSp2 fs04">'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>'."\n";
$code .= '</div>';



$code.='</div>';
$data['str']=$code;
$data['target']='secondtarget';
  
  return $data; 
   
   }
   function pollfunction($pollfid,$productid,$spollid,$valueother)
   {
   
   $arraydata['data']=array();
   
   $sql="select * from tb_plugins_poll where productid=".$productid;
   $arrayplugins=$this->db->db_set_1recordrow($sql);
   
   if($_COOKIE['oauth'])
   {
   $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   
   }
   
   if($arrayplugins['userpoll'])
   {
   
   
   if($_COOKIE['oauth'])
   {
  // $userdata=$this->getuserbyoauth($_COOKIE['oauth']);
   
   if(intval($userdata['roleid'])>=intval($arrayplugins['userpoll']))
   {
   
   
   }else
   {
   $arraydata['error'][]=33;
   return $arraydata;
   }
   
   }else
   {
   $arraydata['error'][]=5;
   return $arraydata;
   }
   
   
   }
   
   if($userdata['userid'])
   {
   $sql='select * from tb_plugins_poll_value where spollid='.$spollid.' and createby='.$userdata['userid'];
   $arrayplugins2=$this->db->db_set_1recordrow($sql);
   if($arrayplugins2['pollvid'])
   {
   
   $arraydata['error'][]=37;
   return $arraydata;
   }
   
   }
   
   
   
   $arraydata3=array();
   $arraydata3['pollfid']=$pollfid;
   $arraydata3['productid']=$productid;
   $arraydata3['spollid']=$spollid;
   $arraydata3['ip']=$_SERVER["REMOTE_ADDR"];
   
      if($userdata['userid'])
   {
   $arraydata3['createby']=$userdata['userid'];
   }else
   {
   $arraydata3['createby']=0;
   
   }
   
   $arraydata3['createdate']=date("Y-m-d H:i:s");
   if($pollfid=='0')
   {
   $arraydata3['value']=$valueother;
   
   }
   
   
   
   $this->db->db_set($arraydata3,'tb_plugins_poll_value');
   
   return $arraydata;
   
   
   
   
   
   }
   
   function pollresultfunction($spollid)
   {
   $arraydata['data']=array();
   $arraydata2=array();
   $sql="select * from tb_plugins_poll_form where spollid=$spollid";
   $arraypollform=$this->db->db_set_recordset($sql);
   $k=0;
   $arraysum=array();
   if(count($arraypollform))
   {
   foreach($arraypollform as $valuepoll)
   {
   

   
   $sql="select count(pollvid) as countpoll from tb_plugins_poll_value where pollfid=".$valuepoll['pollfid'];
   $arraypollvalue=$this->db->db_set_1recordrow($sql);
   $arraydata2[$k]['title']=$valuepoll['value'];
   $arraydata2[$k]['countpoll']=intval($arraypollvalue['countpoll']);
   $arraysum[]=$arraydata2[$k]['countpoll'];
   
   $k++;
   
   }
   
   
   
   }
   
   
   
   $sql="select * from tb_plugins_poll where spollid=$spollid";
   $arraypollvaluecheck=$this->db->db_set_1recordrow($sql);
   
   if($arraypollvaluecheck['other']=='1')
   {
   
   $sql="select count(pollvid) as countpoll from tb_plugins_poll_value where pollfid=0 and spollid=".$spollid;
   $arraypollvalue=$this->db->db_set_1recordrow($sql);
   $arraydata2[$k]['title']='อื่นๆ';
   $arraydata2[$k]['countpoll']=intval($arraypollvalue['countpoll']);
   $arraysum[]=$arraydata2[$k]['countpoll'];
   }
   
   
   $sumall=array_sum($arraysum);
   $k=0;
   if(count($arraydata2))
   {
   foreach($arraydata2 as $valuedata2)
   {
   if($sumall==0)
   {
   $arraydata2[$k]['percent']=0;
   }else
   {
   
   $arraydata2[$k]['percent']=round((100*$valuedata2['countpoll'])/$sumall);
   
   }
   $k++;
   
   }
   
   }
   
   $arraydata['data']=$arraydata2;
   return $arraydata;
   
   
   
   }
  
    function saveplugin($arraydata,$productid,$plugreid)
  {
  $arraydata2=$arraydata;
  $arraydata['productid']=$productid;
  $arraydata['plugreid']=$plugreid;
  
  unset($arraydata['datarowobj']);
  unset($arraydata['deleterowobj']);
  
  if($arraydata['spollid'])
  {
  $spollid=$arraydata['spollid'];
  
  }else
  {
    $sql='SELECT MAX(spollid) as spollid from tb_plugins_poll';
    $arrayspoll=$this->db->db_set_1recordrow($sql);
    $arrayspoll['spollid']=intval($arrayspoll['spollid']);
     $spollid=$arrayspoll['spollid']+1;
  
  }
  $arraydata['spollid']=$spollid;
  
  $this->db->db_set($arraydata,'tb_plugins_poll');
  
  
  if(count($arraydata2['datarowobj']))
  {
  foreach($arraydata2['datarowobj'] as $valuerow)
  {
  
  $arraydata3=array();
  $arraydata3['typeid']=$valuerow['typeid'];
  $arraydata3['value']=$valuerow['value'];
  $arraydata3['spollid']=$spollid;
  $arraydata3['productid']=$productid;
  

  if($valuerow['pollfid'])
  {
  
  $this->db->db_set($arraydata3,'tb_plugins_poll_form',' pollfid='.$valuerow['pollfid']);
  
  
  }else
  {
  
  $this->db->db_set($arraydata3,'tb_plugins_poll_form');
  
  }
  
  }
  
  }
  

    if(count($arraydata2['deleterowobj']))
    {
    foreach($arraydata2['deleterowobj'] as $valuerow)
  {
  if($valuerow['pollfid'])
  {
  $sql="delete from tb_plugins_poll_form where pollfid=".$valuerow['pollfid'];
  $this->db->db_delete($sql);
  
  
  
  $sql="delete from tb_plugins_poll_value where pollfid=".$valuerow['pollfid'];
  $this->db->db_delete($sql);
  
  
  
  }
  
  
  
  }
    
    
    
    }
    
    

  
  
  
  
  }
  
  
    }
?>
